package com.ruoyi.teaching.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.teaching.dto.vo.YaCourseDesignVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaCourseDesignMapper;
import com.ruoyi.teaching.domain.YaCourseDesign;
import com.ruoyi.teaching.service.IYaCourseDesignService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 课程设计Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-11
 */
@Service
public class YaCourseDesignServiceImpl implements IYaCourseDesignService {
  @Autowired
  private YaCourseDesignMapper yaCourseDesignMapper;

  /**
   * 查询课程设计
   *
   * @param id 课程设计主键
   * @return 课程设计
   */
  @Override
  public YaCourseDesign selectYaCourseDesignById(Long id) {
    return yaCourseDesignMapper.selectYaCourseDesignById(id);
  }

  /**
   * 查询课程设计列表
   *
   * @param yaCourseDesign 课程设计
   * @return 课程设计
   */
  @Override
  public List<YaCourseDesign> selectYaCourseDesignList(YaCourseDesign yaCourseDesign) {
    return yaCourseDesignMapper.selectYaCourseDesignList(yaCourseDesign);
  }

  /**
   * 新增课程设计
   *
   * @param yaCourseDesign 课程设计
   * @return 结果
   */
  @Override
  public int insertYaCourseDesign(YaCourseDesign yaCourseDesign) {
    yaCourseDesign.setCreateTime(DateUtils.getNowDate());
    return yaCourseDesignMapper.insertYaCourseDesign(yaCourseDesign);
  }

  /**
   * 修改课程设计
   *
   * @param yaCourseDesign 课程设计
   * @return 结果
   */
  @Override
  public int updateYaCourseDesign(YaCourseDesign yaCourseDesign) {
    yaCourseDesign.setUpdateTime(DateUtils.getNowDate());
    return yaCourseDesignMapper.updateYaCourseDesign(yaCourseDesign);
  }

  /**
   * 批量删除课程设计
   *
   * @param ids 需要删除的课程设计主键
   * @return 结果
   */
  @Override
  public int deleteYaCourseDesignByIds(Long[] ids) {
    return yaCourseDesignMapper.deleteYaCourseDesignByIds(ids);
  }

  /**
   * 删除课程设计信息
   *
   * @param id 课程设计主键
   * @return 结果
   */
  @Override
  public int deleteYaCourseDesignById(Long id) {
    return yaCourseDesignMapper.deleteYaCourseDesignById(id);
  }

  /**
   * 根据阶段id查询课程设计
   *
   * @param stage 阶段id
   * @return 课程设计
   */
  @Override
  public List<YaCourseDesign> selectYaCourseDesignByStageId(Long stage) {
    List<YaCourseDesign> list = yaCourseDesignMapper.selectYaCourseDesignByStageId(stage);
    return CollectionUtils.isEmpty(list)? Collections.emptyList():list;
  }

  /**
   * 批量修改课程设计
   *
   * @param yaCourseDesignVO 课程设计
   * @return 批量修改结果
   */
  @Transactional(rollbackFor = Exception.class)
  @Override
  public int updateYaCourseDesignBatch(YaCourseDesignVO yaCourseDesignVO) {
//    先删除已有课程
    yaCourseDesignMapper.deleteYaCourseDesignByStageId(yaCourseDesignVO.getStage());
//    再批量新增课程
    if (StringUtils.isNotEmpty(yaCourseDesignVO.getCourseList())){
      return yaCourseDesignMapper.insertBatchCourseDesign(yaCourseDesignVO.getCourseList());
    }
    return -1;
  }
}
